package com.example.as_sum.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.as_sum.R;

import org.w3c.dom.Text;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DropDownMenuActivity extends BaseActivity {

    @Bind(R.id.tv_title_menu)
    TextView tvTitleMenu;
    @Bind(R.id.tv_title2_menu)
    TextView tvTitle2Menu;
    @Bind(R.id.tv_title3_menu)
    TextView tvTitle3Menu;
    @Bind(R.id.bg_framelayout)
    FrameLayout bgFramelayout;
    @Bind(R.id.con_framelayout)
    FrameLayout conFramelayout;
    TextView tv_view1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getContentId() {
        return R.layout.activity_drop_down_menu;
    }

    @OnClick({R.id.tv_title_menu, R.id.tv_title2_menu, R.id.tv_title3_menu, R.id.bg_framelayout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_title_menu:
                if (switchMenu(0)) {
                    tvTitleMenu.setTextColor(Color.parseColor("#99CCCC"));
                }
                break;
            case R.id.tv_title2_menu:
                if (switchMenu(1)) {
                    tvTitle2Menu.setTextColor(Color.parseColor("#99CCCC"));
                }
                break;
            case R.id.tv_title3_menu:
                if (switchMenu(2)) {
                    tvTitle3Menu.setTextColor(Color.parseColor("#99CCCC"));
                }
                break;
            case R.id.bg_framelayout:
                closeMenu();
                break;
        }
    }

    @Override
    public void initView() {

//        getLayoutInflater().inflate(R.layout.drop_menu_view1, conFramelayout, true);
//        getLayoutInflater().inflate(R.layout.drop_menu_view2, conFramelayout, true);
//        getLayoutInflater().inflate(R.layout.drop_menu_view3, conFramelayout, true);
        //这两种方法是一样的效果
        View view=getLayoutInflater().inflate(R.layout.drop_menu_view1,conFramelayout,false);
        View view2=getLayoutInflater().inflate(R.layout.drop_menu_view2,conFramelayout,false);
        View view3=getLayoutInflater().inflate(R.layout.drop_menu_view3,conFramelayout,false);
        conFramelayout.addView(view,0);
        conFramelayout.addView(view2,1);
        conFramelayout.addView(view3,2);

        tv_view1=ButterKnife.findById(view,R.id.view1_tv);
        tv_view1.setText("实打实大苏打");
        tv_view1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView tv= (TextView) v;
                tv.setText("你单击了这个view");
                Toast.makeText(DropDownMenuActivity.this,"你单击了这个第一个文本",Toast.LENGTH_LONG).show();
                //closeMenu();
            }
        });
    }

    private void closeMenu() {
        resetTitle();
        conFramelayout.setAnimation(AnimationUtils.loadAnimation(this, R.anim.drop_menu_out));
        conFramelayout.setVisibility(View.INVISIBLE);
        bgFramelayout.setAnimation(AnimationUtils.loadAnimation(this, R.anim.drop_menu_bg_out));
        bgFramelayout.setVisibility(View.INVISIBLE);
    }

    private void showMenu(int index) {
        int count = conFramelayout.getChildCount();
        int height = 0;
        for (int i = 0; i < count; i++) {
            View view = conFramelayout.getChildAt(i);
            if (i == index) {
                view.setVisibility(View.VISIBLE);
                height = view.getMeasuredHeight();
                Log.e("----->", "view的高度:" + height);
            } else {
                view.setVisibility(View.INVISIBLE);
            }
        }
        FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, height);
        conFramelayout.setLayoutParams(param);

        conFramelayout.setAnimation(AnimationUtils.loadAnimation(this, R.anim.drop_menu_in));
        conFramelayout.setVisibility(View.VISIBLE);
        bgFramelayout.setAnimation(AnimationUtils.loadAnimation(this, R.anim.drop_menu_bg_in));
        bgFramelayout.setVisibility(View.VISIBLE);
    }

    private boolean switchMenu(int index) {
        boolean isShow = false;
        if (conFramelayout.getVisibility() == View.VISIBLE) {
            closeMenu();
            isShow = false;
        } else if (conFramelayout.getVisibility() == View.INVISIBLE) {
            showMenu(index);
            isShow = true;
        }
        return isShow;
    }

    private void resetTitle() {
        tvTitleMenu.setTextColor(Color.parseColor("#000000"));
        tvTitle2Menu.setTextColor(Color.parseColor("#000000"));
        tvTitle3Menu.setTextColor(Color.parseColor("#000000"));
    }

    @Override
    public void addListener() {

    }

    @Override
    public void loadData() {

    }

    @Override
    public Handler getHandler() {
        return null;
    }

    @Override
    public void onBackPressed() {
        if (conFramelayout.getVisibility() == View.VISIBLE)
            closeMenu();
        else
            super.onBackPressed();
    }
}
